//
//  ZMYAnimator.m
//  奇阅
//
//  Created by 奇阅科技 on 14-10-16.
//  Copyright (c) 2014年 奇阅科技. All rights reserved.
//

#import "ZMYAnimator.h"
#import "BookReadDownloadViewController.h"
@interface ZMYAnimator()
{
    UIView *_currentView;
    UIImageView *_currentImageView;
    NSLayoutConstraint *_currentViewWidthLayoutConst;
    UIButton *_coverButton;
}
@end
@implementation ZMYAnimator
-(NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{
    return 0.5;
}
-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
    UIViewController* toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIViewController* fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    [[transitionContext containerView] addSubview:toViewController.view];
    if (self.operation == UINavigationControllerOperationPush) {
        if ([toViewController isKindOfClass:[BookReadDownloadViewController class]]) {
            [self resetCurrentViewToRootViewController:fromViewController toView:toViewController];
            toViewController.view.frame = CGRectMake(toViewController.view.frame.size.width, 0, toViewController.view.frame.size.width, toViewController.view.frame.size.height);
            [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
                fromViewController.view.frame = CGRectMake(-(fromViewController.view.frame.size.width), 0, fromViewController.view.frame.size.width, fromViewController.view.frame.size.height);
                _currentViewWidthLayoutConst.constant = fromViewController.view.frame.size.width+54;
                [_currentView layoutIfNeeded];
                toViewController.view.frame = CGRectMake(0, 0, toViewController.view.frame.size.width, toViewController.view.frame.size.height);
            } completion:^(BOOL finished) {
                [self showShadow:YES];
                fromViewController.view.transform = CGAffineTransformIdentity;
                [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
            }];
        } else {
            toViewController.view.frame = CGRectMake(-toViewController.view.frame.size.width, 0, toViewController.view.frame.size.width, toViewController.view.frame.size.height);
            [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
                fromViewController.view.frame = CGRectMake(fromViewController.view.frame.size.width, 0, fromViewController.view.frame.size.width, fromViewController.view.frame.size.height);
                toViewController.view.frame = CGRectMake(0, 0, toViewController.view.frame.size.width, toViewController.view.frame.size.height);
            } completion:^(BOOL finished) {
                fromViewController.view.transform = CGAffineTransformIdentity;
                [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
            }];
        }
    } else {
        if ([fromViewController isKindOfClass:[BookReadDownloadViewController class]]) {
            toViewController.view.frame = CGRectMake(-(toViewController.view.frame.size.width-54), 0, toViewController.view.frame.size.width, toViewController.view.frame.size.height);
            [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
                fromViewController.view.frame = CGRectMake(fromViewController.view.frame.size.width, 0,
                                                           fromViewController.view.frame.size.width, fromViewController.view.frame.size.height);
                _currentViewWidthLayoutConst.constant = fromViewController.view.frame.size.width;
                [_currentView layoutSubviews];
                toViewController.view.frame = CGRectMake(0, 0, toViewController.view.frame.size.width, toViewController.view.frame.size.height);
            } completion:^(BOOL finished) {
                fromViewController.view.transform = CGAffineTransformIdentity;
                [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
            }];
        } else {
            toViewController.view.frame = CGRectMake(toViewController.view.frame.size.width, 0, toViewController.view.frame.size.width, toViewController.view.frame.size.height);
            [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
                fromViewController.view.frame = CGRectMake(-fromViewController.view.frame.size.width, 0, fromViewController.view.frame.size.width, fromViewController.view.frame.size.height);
                toViewController.view.frame = CGRectMake(0, 0, toViewController.view.frame.size.width, toViewController.view.frame.size.height);
            } completion:^(BOOL finished) {
                fromViewController.view.transform = CGAffineTransformIdentity;
                [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
                
            }];
        }
        
    }
    
}
-(void)resetCurrentViewToRootViewController:(UIViewController *)fromView toView:(UIViewController *)toView{
    if (!_currentView) {
        CGRect frame = CGRectZero;
        frame = fromView.view.bounds;
        _currentView = [[UIView alloc]initWithFrame:CGRectMake(-frame.size.width, 0, frame.size.width, frame.size.height)];
        _currentImageView = [[UIImageView alloc]initWithFrame:_currentView.bounds];
        [_currentView addSubview:_currentImageView];
        _currentImageView.translatesAutoresizingMaskIntoConstraints = NO;
        NSDictionary *views = NSDictionaryOfVariableBindings(_currentImageView);
        [_currentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_currentImageView]|" options:0 metrics:nil views:views]];
        [_currentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_currentImageView]|" options:0 metrics:nil views:views]];
        
        
        _coverButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _coverButton.frame = _currentView.bounds;
        [_coverButton addTarget:toView action:@selector(hideSideViewController) forControlEvents:UIControlEventTouchDown];
        [_currentView addSubview:_coverButton];
        _coverButton.translatesAutoresizingMaskIntoConstraints = NO;
        views = NSDictionaryOfVariableBindings(_coverButton);
        [_currentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_coverButton]|" options:0 metrics:nil views:views]];
        [_currentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_coverButton]|" options:0 metrics:nil views:views]];
        [self showShadow:YES];
        _currentView.translatesAutoresizingMaskIntoConstraints = NO;
        _currentViewWidthLayoutConst = [NSLayoutConstraint constraintWithItem:_currentView
                                                                    attribute:NSLayoutAttributeWidth
                                                                    relatedBy:NSLayoutRelationEqual
                                                                       toItem:nil
                                                                    attribute:NSLayoutAttributeNotAnAttribute
                                                                   multiplier:1.0
                                                                     constant:fromView.view.frame.size.width];
        [_currentView addConstraint:_currentViewWidthLayoutConst];//设置子视图的宽度和父视图的宽度相同
        
        [_currentView addConstraint:[NSLayoutConstraint constraintWithItem:_currentView
                                                                attribute:NSLayoutAttributeHeight
                                                                relatedBy:NSLayoutRelationEqual
                                                                   toItem:nil
                                                                attribute:NSLayoutAttributeNotAnAttribute
                                                               multiplier:1.0
                                                                 constant:fromView.view.frame.size.height]];//设置子视图的高度是父视图高度的相同
    }
    _currentImageView.image = [self getImageFromView:fromView.view];
    [toView.view addSubview:_currentView];
    [toView.view addConstraint:[NSLayoutConstraint constraintWithItem:_currentView
                                                            attribute:NSLayoutAttributeTop
                                                            relatedBy:NSLayoutRelationEqual
                                                               toItem:toView.view
                                                            attribute:NSLayoutAttributeTop
                                                           multiplier:1.0
                                                             constant:0]];//设置子视图的高度是父视图高度的相同
    [toView.view addConstraint:[NSLayoutConstraint constraintWithItem:_currentView
                                                            attribute:NSLayoutAttributeLeft
                                                            relatedBy:NSLayoutRelationEqual
                                                               toItem:toView.view
                                                            attribute:NSLayoutAttributeLeft
                                                           multiplier:1.0
                                                             constant:-fromView.view.frame.size.width]];//设置子视图的高度是父视图高度的相同
    [_currentView layoutIfNeeded];
//    NSDictionary *views = NSDictionaryOfVariableBindings(_currentView);
//    [toView.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(-_currentView.frame.size.width)[_currentView]" options:0 metrics:nil views:views]];
//    [toView.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_currentView]|" options:0 metrics:nil views:views]];
}
-(UIImage *)getImageFromView:(UIView *)theView{
    
    UIGraphicsBeginImageContext(theView.bounds.size);
    
//    UIGraphicsBeginImageContextWithOptions(theView.bounds.size, YES, theView.layer.contentsScale);
    
    [theView.layer renderInContext:UIGraphicsGetCurrentContext()];
    
    UIImage *image=UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return image;
    
}
- (void)showShadow:(BOOL)show{
    _currentView.layer.shadowOpacity    = show ? 0.8f : 0.0f;
    if (show) {
        _currentView.layer.cornerRadius = 4.0f;
        _currentView.layer.shadowOffset = CGSizeZero;
        _currentView.layer.shadowRadius = 4.0f;
        _currentView.layer.shadowPath   = [UIBezierPath bezierPathWithRect:_currentView.bounds].CGPath;
    }
}
-(void)hideSideViewController{
    NSLog(@"sdfadfas");
}
@end
